﻿using AdminSystem.Application.Commands.TraceabilityCommands;
using AdminSystem.Application.Dtos;
using AdminSystem.Domain.Entities;
using AdminSystem.EntityFramworkCore;
using AdminSystem.Repositories.Interfaces;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Application.CommandHandlers.TraceabilityCommandHandlers
{
    public class TracebilitysDetileCommandsHandlers : IRequestHandler<TracebilitysDetileCommands, APIResult<TraceabilityDTO>>
    {
        private readonly IBaseRepositories<Traceability> repository;
        private readonly IBaseRepositories<Shop> shoppRepository;
        public TracebilitysDetileCommandsHandlers(IBaseRepositories<Traceability> repository, IBaseRepositories<Shop> shoppRepository)
        {
            this.repository = repository;
            this.shoppRepository = shoppRepository;
        }
        public Task<APIResult<TraceabilityDTO>> Handle(TracebilitysDetileCommands request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<TraceabilityDTO> result = new APIResult<TraceabilityDTO>();
                result.Code = ResultCode.查询成功;
                result.Message = ResultCode.查询成功.ToString();
                var list = from a in repository.GetAll().Result
                           join b in shoppRepository.GetAll().Result on a.ShopId equals b.ShopId
                           where a.IsDelete == false
                           select new TraceabilityDTO
                           {
                               Id = a.Id,
                               TraceabilityId = a.TraceabilityId,
                               ShopId = a.ShopId,
                               ShopName = b.ShopName,
                               ProBatch = a.ProBatch,
                               ShelfLife = a.ShelfLife,
                               ProLicense = a.ProLicense,
                               Manufacturers = a.Manufacturers,
                               CoverPic = a.CoverPic,
                               IsDelete = a.IsDelete
                           };
                var entity = list.Where(x => x.TraceabilityId == request.TraceabilityId);
                result.Data = entity.FirstOrDefault();
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
